package com.kedacom.freedomLabel.model.db.dao;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
import android.arch.persistence.room.Query;

import com.kedacom.freedomLabel.model.bean.Business;
import com.kedacom.freedomLabel.model.bean.BusinessCount;
import com.kedacom.freedomLabel.model.bean.BusinessGroup;

import java.util.List;

/**
 * @author licheng
 * @since 2019/10/25 14:03
 */
@Dao
public interface BusinessDao extends IDao<Business> {

    @Override
    @Query("SELECT COUNT(0) FROM business")
    int count();

    @Override
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    long save(Business business);

    @Override
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    long[] save(List<Business> businesses);

    @Override
    @Query("SELECT * FROM business")
    List<Business> get();

    @Override
    @Query("SELECT * FROM business WHERE `index` =:param")
    List<Business> get(String param);

    @Override
    @Query("SELECT * FROM business WHERE `index` =:param1 OR `index` =:param2")
    List<Business> get(String param1, String param2);

    @Query("DELETE FROM business WHERE `index` IN (:ids)")
    void deleteByIds(List<Integer> ids);

    @Query("DELETE FROM business WHERE createTime>=:startTime AND createTime<:endTime AND product_id = :productId")
    void deleteProduct(String productId, long startTime, long endTime);

    @Query("DELETE FROM business WHERE  createTime>=:startTime AND createTime<:endTime AND cardNumber =:cardNumber")
    void deleteCardNumber(String cardNumber, long startTime, long endTime);

    @Query("DELETE FROM business WHERE  createTime>=:startTime AND createTime<:endTime AND cardNumber =:cardNumber AND product_id=:productId")
    void deleteProductAndCardNumber(String productId, String cardNumber, long startTime, long endTime);

    @Query("DELETE FROM business WHERE  createTime>=:startTime AND createTime<:endTime")
    void delete(long startTime, long endTime);

    @Override
    @Delete
    void delete(Business business);

    @Override
    @Query("DELETE FROM business")
    void delete();

    @Override
    @Query("DELETE FROM business WHERE category =:category")
    void delete(String category);

    @Query("SELECT * FROM business WHERE createTime >=:startTime order by createTime DESC")
    List<Business> getBusinessWithStartTime(long startTime);

    @Query("DELETE FROM business WHERE createTime >=:startTime")
    void deleteWithStartTime(long startTime);

    @Query("SELECT COUNT(*) as number,SUM(count) as count,SUM(netWeight) as weight FROM business WHERE createTime>=:startTime")
    BusinessCount getBusinessCount(long startTime);

    @Query("SELECT COUNT(*) as number,SUM(count) as count,SUM(netWeight) as weight FROM business WHERE createTime>=:startTime AND createTime<:endTime")
    BusinessCount getBusinessCount(long startTime, long endTime);

    @Query("SELECT product_id, name,COUNT(*) as number,SUM(count) as count,SUM(netWeight) as weight FROM business WHERE product_id=:productId AND createTime>=:startTime AND createTime<:endTime GROUP BY product_id ")
    List<BusinessGroup> getBusinessGroup(String productId, long startTime, long endTime);

    @Query("SELECT product_id, name,COUNT(*) as number,SUM(count) as count,SUM(netWeight) as weight FROM business WHERE createTime>=:startTime AND createTime<:endTime GROUP BY product_id ")
    List<BusinessGroup> getBusinessGroup(long startTime, long endTime);

    @Query("SELECT cardNumber as product_id, operator_name as name,COUNT(*) as number,SUM(count) as count,SUM(netWeight) as weight FROM business WHERE cardNumber=:cardNumber AND createTime>=:startTime AND createTime<:endTime GROUP BY cardNumber ")
    List<BusinessGroup> getBusinessGroupWithOperator(String cardNumber, long startTime, long endTime);

    @Query("SELECT cardNumber as product_id, operator_name as name,COUNT(*) as number,SUM(count) as count,SUM(netWeight) as weight FROM business WHERE createTime>=:startTime AND createTime<:endTime GROUP BY cardNumber ")
    List<BusinessGroup> getBusinessGroupWithOperator(long startTime, long endTime);

    @Query("SELECT * FROM business WHERE createTime>=:startTime AND createTime<:endTime ORDER BY createTime DESC")
    List<Business> getBusinessList(long startTime, long endTime);

    @Query("SELECT * FROM business WHERE createTime>=:startTime AND createTime<:endTime AND cardNumber=:cardNumber ORDER BY createTime DESC")
    List<Business> getBusinessListWithOperator(String cardNumber, long startTime, long endTime);

    @Query("SELECT * FROM business WHERE createTime>=:startTime AND createTime<:endTime AND product_id=:productId ORDER BY createTime DESC")
    List<Business> getBusinessList(long startTime, long endTime, String productId);

    @Query("SELECT * FROM business WHERE createTime>=:startTime AND createTime<:endTime AND product_id=:productId AND cardNumber=:cardNumber ORDER BY createTime DESC")
    List<Business> getBusinessListProductIdAndCardNumber(long startTime, long endTime, String productId, String cardNumber);

    @Query("SELECT * FROM business GROUP BY product_id")
    List<Business> getBusinessCategories();

    @Query("SELECT * FROM business GROUP BY cardNumber")
    List<Business> getBusinessOperators();
}